package com.leancloud.circle.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSONArray;
import com.aries.ui.view.radius.RadiusEditText;
import com.aries.ui.view.radius.RadiusTextView;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVObject;
import com.leancloud.circle.R;
import com.leancloud.circle.adapter.CircleCommentAdapter;
import com.leancloud.circle.adapter.CirclePhotoAdapter;
import com.leancloud.circle.bean.CircleBean;
import com.leancloud.circle.bean.CollectionBean;
import com.leancloud.circle.bean.CommentBean;
import com.leancloud.circle.manage.CircleBack;
import com.leancloud.circle.manage.CircleGlobal;
import com.leancloud.circle.manage.CirclePathManage;

import java.util.List;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.ToastUtil;

@Route(path = CirclePathManage.CIRCLE_ARTICLE_DETAILS)
public class CircleDetailsActivity extends BaseTitleActivity {

    private ImageView headView;
    private TextView userNameTx, dateTx, contentTx, collectionTx, commentTx, likeTx, browseTx;
    private int circleId;
    private LinearLayout communityContentLayout;

    private CircleCommentAdapter commentAdapter;
    private RecyclerView commitRecyclerView;

    private FrameLayout commentInputLt;
    private RadiusTextView commentSubmitTx;
    private InputMethodManager inputMethodManager;
    private RadiusEditText commentEt;

    @Override
    public void setTitleBar(TitleBarView titleBar) {

    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_circle_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        headView = findViewById(R.id.circle_details_user_head);
        userNameTx = findViewById(R.id.circle_details_user_name);
        dateTx = findViewById(R.id.circle_details_date);
        contentTx = findViewById(R.id.circle_details_content);
        collectionTx = findViewById(R.id.circle_details_collection);
        commentTx = findViewById(R.id.circle_details_comment);
        likeTx = findViewById(R.id.circle_details_like);
        browseTx = findViewById(R.id.circle_details_browse_tx);

        commitRecyclerView = findViewById(R.id.circle_details_commit_recycler);
        communityContentLayout = findViewById(R.id.community_content_layout);

        commentInputLt = mContentView.findViewById(R.id.comment_input_layout);
        commentEt = mContentView.findViewById(R.id.comment_input_content);
        commentSubmitTx = mContentView.findViewById(R.id.comment_input_submit);
        inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

        circleId = getIntent().getIntExtra("communityId", 0);
        if (circleId != 0) {
            requestData();
        }

        commentAdapter = new CircleCommentAdapter(R.layout.circle_layout_item_comment);
        commitRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        commitRecyclerView.setAdapter(commentAdapter);

        commentSubmitTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String comment = commentEt.getText().toString();
                if (comment.isEmpty()) {
                    ToastUtil.show(getResources().getString(R.string.circle_content_hint));
                } else {
//                    CircleGlobal.getCircleImp().commentCircle(UserGlobal.getCurrentUser().getToken(), circleId, comment, -1, new CircleBack() {
//                        @Override
//                        public void onBack(Object o, String err) {
//                            if (err == null) {
//                                commentEt.setText("");
//                                inputMethodManager.hideSoftInputFromWindow(commentEt.getWindowToken(), 0);
//                                requestData();
//                            } else {
//                                ToastUtil.show("评论未成功");
//                            }
//                        }
//                    });
                }
            }
        });

        adaptiveKeyboardPushUp();
    }

    private void adaptiveKeyboardPushUp() {
        View contentView = findViewById(R.id.circle_content_view);
        //ViewTreeObserver.OnGlobalLayoutListener listener = WindowsUtil.getNoNavigateGlobalLayoutListener(commentEt, contentView, this, commentInputLt);
        //commentEt.getViewTreeObserver().addOnGlobalLayoutListener(listener);
    }

    private void requestData() {
        CircleGlobal.getCircleImp().requestCircleDetails(circleId, new CircleBack<CircleBean>() {
            @Override
            public void onBack(CircleBean bean, String err) {
                if (err == null && bean != null) {
                    setViewData(bean);
                    communityContentLayout.setVisibility(View.VISIBLE);
                } else {
                    communityContentLayout.setVisibility(View.GONE);
                }
            }
        });
        CircleGlobal.getCircleImp().requestComment(1, 20, null, circleId, -1, new CircleBack<List<CommentBean>>() {
            @Override
            public void onBack(List<CommentBean> beans, String err) {
                if (err == null && beans != null) {
                    commentAdapter.replaceData(beans);
                    commentAdapter.notifyDataSetChanged();
                }
            }
        });

    }

    private void setViewData(final CircleBean bean) {
        String nike = bean.getUser().getUser_nickname();
        String headUrl = bean.getUser().getAvatar();

        String date = bean.getCreate_time();
        String content = bean.getPost_content();
        List<String> images = JSONArray.parseArray(bean.getMore(), String.class);
        String commentCount = String.valueOf(bean.getComment_count());
        String likeCount = String.valueOf(bean.getPost_like());
        String browseCount = String.valueOf(bean.getPost_hits());

        RecyclerView imagesRecycler = findViewById(R.id.circle_details_image_recycler);
        CirclePhotoAdapter photoAdapter = new CirclePhotoAdapter(R.layout.circle_layout_item_circle_photo);
        imagesRecycler.setAdapter(photoAdapter);
        imagesRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        if (images !=null && !images.isEmpty()){
            photoAdapter.replaceData(images);
            photoAdapter.notifyDataSetChanged();
        }
//        GlideManager.loadCircleImg(headUrl, headView, R.mipmap.icon_default_head);
        userNameTx.setText(nike);
        dateTx.setText(date);
        contentTx.setText(content);
        collectionTx.setText("");
        commentTx.setText(commentCount);
        likeTx.setText(likeCount);
        browseTx.setText(browseCount);

//        likeTx.setSelected(UserGlobal.getInstance().containLike(String.valueOf(bean.getUser().getId())));
//        if (UserGlobal.getInstance().containLike(String.valueOf(bean.getUser().getId())))
//            likeTx.setEnabled(false);

        collectionTx.setSelected(CircleGlobal.isCollections(bean.getId()));

//        commentTx.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                LoginCallUtil.verify_Toset(new LoginCallUtil.Back() {
//                    @Override
//                    protected void passBack() {
//                        commitEvent();
//                    }
//                });
//            }
//        });

//        likeTx.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                LoginCallUtil.verify_Toset(new LoginCallUtil.Back() {
//                    @Override
//                    protected void passBack() {
////                        handlerLikeClick(likeTx.isSelected(), object);
//                    }
//                });
//            }
//        });
//        collectionTx.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                LoginCallUtil.verify_Toset(new LoginCallUtil.Back() {
//                    @Override
//                    protected void passBack() {
//                        handlerCollectionClick(bean);
//                    }
//                });
//            }
//        });
    }

    private void handlerCollectionClick(CircleBean bean) {
        if (bean == null)
            return;
        if (!collectionTx.isSelected()) {
//            CircleGlobal.getCircleImp().addCollection(UserGlobal.getCurrentUser().getToken(), bean.getId(), new CircleBack() {
//                @Override
//                public void onBack(Object o, String err) {
//                    if (err == null) {
//                        ToastUtil.show("收藏成功");
//                        CircleGlobal.updateCollection();
//                    }
//                }
//            });
        } else {
            CollectionBean collection = CircleGlobal.getCollection(bean.getId());
//            CircleGlobal.getCircleImp().removeCollection(UserGlobal.getCurrentUser().getToken(), collection.getId(), new CircleBack() {
//                @Override
//                public void onBack(Object o, String err) {
//                    if (err == null) {
//                        CircleGlobal.updateCollection();
//                    }
//                }
//            });
        }
        collectionTx.setSelected(!collectionTx.isSelected());
    }

    private void handlerLikeClick(boolean isLike, AVObject object) {
//        if (!isLike) {
//            AVObject like = new AVObject(TABLE_LIKE);
//            like.put(LIKE_COMMUNITY, object.getObjectId());
//            like.put(LIKE_USER, AVUser.getCurrentUser().getObjectId());
//            AVGlobal.getImpl().addLike(like, null);
//            int likeCount = Integer.parseInt(likeTx.getText().toString());
//            likeTx.setText(likeCount + 1 + "");
//            likeTx.setSelected(true);
////            UserGlobal.getInstance().updateLike();
//        }
    }

    private void commitEvent() {
        commentInputLt.setVisibility(View.VISIBLE);
        commentEt.setFocusable(true);
        commentEt.setFocusableInTouchMode(true);
        commentEt.requestFocus();
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
